﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using BackupObjects.Taging;
using Microsoft.Samples.ServiceHosting.StorageClient;
using NUnit.Framework;

namespace BackupObjects.Test
{
    [TestFixture]
    public class BackupRepositoryTests
    {
        private static TagRepository _tagRepository;
        private static BackupRepository _backupRepository;

        const string Username = BackupRepository.UserNameAdmin;

        private Tag tag1, tag11, tag12, tag21;
        private BackupObject backupObj1, backupObj2, backupObj3;

        [SetUp]
        public void TagRepositorySetUp()
        {
            //Add tags.
            _tagRepository = new TagRepository();
            _backupRepository=new BackupRepository();
            

            _tagRepository
                .LoadAll()
                .ForEach(item=>_tagRepository.Delete(item.Id));

            _backupRepository.LoadAll(Username);
            _backupRepository.Backups.ForEach(item => _backupRepository.Delete(item.ID,Username));



            tag1 = new Tag() { Id = Guid.NewGuid(), Name = "Tag1Name" };
            tag11 = new Tag() { Id = Guid.NewGuid(), Name = "Tag11Name", Parent = tag1 };
            tag12 = new Tag() { Id = Guid.NewGuid(), Name = "Tag12Name", Parent = tag1 };
            tag21 = new Tag() { Id = Guid.NewGuid(), Name = "Tag2Name", Parent = tag1 };

            _tagRepository.Save(tag1);
            _tagRepository.Save(tag11);
            _tagRepository.Save(tag12);
            _tagRepository.Save(tag21);

            //Add backup obj.
            backupObj1 = new BackupObject() { ID = Guid.NewGuid(), Name = "backupObj1", Contents = new byte[] { 1, 2 }, TagIds = new List<Guid>() { tag12 } };
            backupObj2 = new BackupObject() { ID = Guid.NewGuid(), Name = "backupObj2", Contents = new byte[] { 1, 2 }, TagIds = new List<Guid>() { tag21, tag12 } };
            backupObj3 = new BackupObject() { ID = Guid.NewGuid(), Name = "backupObj3", Contents = new byte[] { 1, 2 }, TagIds = new List<Guid>() { tag21 } };

            _backupRepository.Save(backupObj1);
            _backupRepository.Save(backupObj2);
            _backupRepository.Save(backupObj3);

        }

        [Test]
        public void LoadBackupObjTest()
        {
            BackupRepository backupRepository = new BackupRepository();
            var backupObject= backupRepository.Load(backupObj3.ID, Username);

            Assert.IsNotNull(backupObject);
            Assert.AreEqual(backupObj3.TagIds.Count,backupObject.TagIds.Count,"The number of tags is not valid.");
            Assert.AreEqual(backupObj3.TagIds[0], backupObject.TagIds[0],"The tag id is not valid.");
        }

        [Test]
        public void GetBackupEntitiesByTagOneObjectTest()
        {
            BackupRepository backupRepository=new BackupRepository();
            var backupObjs= backupRepository.GetBackupEntitiesByTag(tag21.Id);            

            Assert.IsNotNull(backupObjs, "The list of items cannot be null.");
            Assert.AreEqual(2, backupObjs.Count, "The item number is invalid.");
        }

        [Test]
        public void GetBackupEntitiesByTagsOneObjectTest()
        {
            BackupRepository backupRepository = new BackupRepository();
            var backupObjs = backupRepository.GetBackupEntitiesByTags(new List<Guid>(){ tag21.Id,tag12.Id});

            Assert.IsNotNull(backupObjs, "The list of items cannot be null.");
            Assert.AreEqual(1, backupObjs.Count, "The item number is invalid.");
        }


    }
}
